package pl.mchaniewski.ryneczek.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pl.mchaniewski.ryneczek.dao.PortfolioDao;
import pl.mchaniewski.ryneczek.pojo.Portfolio;

@Repository("portfolioDao")
@Transactional(readOnly = true)
public class HibernatePortfolioDao extends
        HibernateGenericDao<Portfolio, Integer> implements PortfolioDao {

    @SuppressWarnings("unchecked")
    @Override
    public List<Portfolio> findByUserId(Integer id) {
        String queryString = "from Portfolio p where p.user.userid=:id";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("id", id);

        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Portfolio> findByUsername(String name) {
        String queryString = "from Portfolio p where p.user.login=:name";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("name", name);

        return query.getResultList();
    }

}
